package com.sysolve.betterCity.common;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;

public class Help {
	
	public static String WEBAPP_URL = "http://www.sysolve.com/BetterCity";	//change this to your own hosted webapp url
	 
	public static long selectPlaceCount = 0;
	/**
	 * 解析地点
	 * @param lng
	 * @param lat
	 * @return
	 */	
	public static String selectPlace(int lng, int lat) {
		HttpClient httpclient = new HttpClient();

		String URL = "http://ls.vip.51ditu.com/mosp/gc?pos="
				+ lng + "," + lat;

		GetMethod get = new GetMethod(URL);
		try {
			httpclient.executeMethod(get);
		} catch (HttpException e1) {
			Help.log(e1);
		} catch (IOException e1) {
			Help.log(e1);
		}
		Help.log(get.getStatusLine().toString());
		String result = "";
		try {
			result = get.getResponseBodyAsString();
			selectPlaceCount++;
		} catch (Exception e1) {
			Help.log(e1);
		}		
		get.releaseConnection();
		
		if (result.contains("<msg>"))
			result=result.replaceAll(".*<msg>", "").replaceAll("</msg>.*", "").trim();
		else
			result=" ";
		Help.log(result);

		return result;
	}
	
	public static void log(String st) {
		System.out.println(st);
	}
	public static void log(Throwable e) {
		e.printStackTrace(System.err);
	}
	
	public static DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
	public static DateFormat DATETIME_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm");
	
	public static int getInt(String st) {
		try {
			return Integer.parseInt(st);
		} catch (Exception e) {
			return 0;
		}
	}
	
	public static Date getDate(String st) {
		try {
			return DATE_FORMAT.parse(st);
		} catch (Exception e) {
			return new Date();
		}
	}
	

}
